    <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="/favicon.ico" >
    <link rel="Shortcut Icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="/public/js/html5shiv.js"></script>
    <script type="text/javascript" src="/public/js/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/Hui-iconfont/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/css/style.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/laydate/skins/default/laydate.css"/>
    <!--[if IE 6]>
    <script type="text/javascript" src="/public/js/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>快方管理后台</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <style type="text/css">
        .date{
            width: 100px;
        }
        .table .show_count{
            text-align: right;
            padding-right: 10px;
        }
        .search{
            width: 250px;
        }
    </style>
</head>
<body class="big-page">
<div class="Hui-article">
    <article class="cl pd-20">
        <div class="control">
            <a href="#" class="clone_product btn btn-primary radius">克隆药品</a>
            &emsp;
            <a href="#" class="append_product btn btn-info radius">添加药品</a>
        </div>
        <div class="mt-10">
            <table class="table table-border table-bordered table-bg table-hover table-sort table-responsive" id="productList">
                <thead>
                    <tr>
                        <td colspan="3"><input type="text" class="search input-text radius" name="search"></td> 
                        <td colspan="1" class="show_count"></td> 
                    </tr>
                    <tr class="text-c">
                        <th>#</th>
                        <th>药品id</th>
                        <th>药品名称</th>
                        <th>编辑</th>
                    </tr>
                </thead>
                
                <tbody>

                </tbody>
            </table>
        </div>
    </article>
    <input type="hidden" name="store_id" value="<?=$_GET['store_id']?>">
</div>

<script type="text/javascript" src="/public/js/jquery.min.js"></script>
<script type="text/javascript" src="/public/plugins/layer/layer.js"></script>
<script type="text/javascript" src="/public/plugins/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/public/plugins/h-ui.admin/js/H-ui.admin.js"></script>
<script type="text/javascript" src="/public/plugins/jquery.contextmenu/jquery.contextmenu.r2.js"></script>
<script type="text/javascript" src="/public/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/public/plugins/laydate/laydate.js"></script>
<script type="text/javascript">
    $.ajax({
        url : window.location.href,
        type : "POST",
        dataType : "json",
        success : function(res){
            var html = "";
            var i = 1;

            for(k in res.data){
                var data = res.data[k];

                html += "<tr>";
                html += "<td>" + i + "</td>";
                html += "<td>" + data.product_id + "</td>";
                html += "<td>" + data.product_name + "</td>";
                html += "<td><a href='#' class='remove_product' data-product_id='" + data.product_id + "'>删除</a></td>";
                html += "</tr>";

                i++;
            }

            $(".show_count").html("共&emsp;" + (i-1) + "&emsp;条记录")
            $("#productList tbody").html(html);
        }
    })

    $("input[name='search']").bind("keyup",function() {
        var text = $(this).val();

        $("#productList tbody tr").hide();

        if(text.length > 0){
            $("#productList tbody tr").each(function(){
                var product_name = $("td:eq(2)", this).text();
                var product_id = $("td:eq(1)", this).text();

                if(product_name.indexOf(text) > -1){
                    $(this).show();
                }

                if(product_id == text){
                    $(this).show();
                }
            })
        }else{
            $("#productList tbody tr").show();
        }
    })

    $(".clone_product").bind("click", function(){
        layer.prompt({
            title: '药店编号', 
            formType: 0, 
            shadeClose: true
        }, 
        function(text, index){
            var store_id = parseInt(text);
            var this_store_id = $("input[name='store_id'").val();

            if(isNaN(store_id)){
                layer.alert("请输入正确的药店id");
                return false;
            }

            if(store_id == this_store_id){
                layer.alert("请勿输入当前药店id");
                return false;
            }
            $.ajax({
                url: "/StoreProduct/cloneProduct",
                type: "GET",
                dataType: "json",
                data: {"source_id" : store_id, "target_id" : this_store_id},
                success: function(res){
                    layer.alert(res.info, function(){
                        parent.dataTables.draw(false);
                        window.location.reload();
                    });
                    
                }
            })
        });
    })

    $(".append_product").bind("click", function(){
        layer.prompt({
            title: '药品编号', 
            formType: 2, 
            shadeClose: true,
            maxlength: 9999999
        }, 
        function(text, index){
            var this_store_id = $("input[name='store_id'").val();

            if(text.length < 1){
                layer.alert("请输入商品id");
                return false;
            }
            $.ajax({
                url: "/StoreProduct/appendProduct",
                type: "POST",
                dataType: "json",
                data: {"store_id" : this_store_id, "product_id" : text},
                success: function(res){
                    layer.alert(res.info, function(){

                        parent.dataTables.draw(false);
                        window.location.reload();
                    });
                    
                }
            })
        });
    })

    $(document).on("click", ".remove_product", function(){
        var store_id = $("input[name='store_id'").val();
        var product_id = $(this).data("product_id");
        var this_row = $(this).parent().parent();

        $.ajax({
            url: "/StoreProduct/removeProduct",
            type: "POST",
            dataType: "json",
            data: {"store_id" : store_id, "product_id" : product_id},
            success: function(res){
                parent.dataTables.draw(false);

                if(res.status == "success"){
                    this_row.fadeTo("slow", 0.01, function(){//fade
                        this_row.slideUp("slow", function() {//slide up
                            this_row.remove();//then remove from the DOM
                            var cnt = $("#productList tbody tr").length;
                            $(".show_count").html("共&emsp;" + cnt + "&emsp;条记录")
                        });
                    });

                    
                }
            }
        })
    })
</script>
</body>
</html>
